package com.zy.experience_system.service.impl;

import com.zy.experience_system.entity.StuAndSet;
import com.zy.experience_system.entity.TestSet;
import com.zy.experience_system.enums.ResultEnum;
import com.zy.experience_system.exception.CommonException;
import com.zy.experience_system.form.TestSetForm;
import com.zy.experience_system.repository.StuAndSetRepository;
import com.zy.experience_system.repository.TestSetRepository;
import com.zy.experience_system.service.TestSetService;
import com.zy.experience_system.util.MyDateUtil;
import com.zy.experience_system.vo.StuSetVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TestSetServiceImpl implements TestSetService {

    @Autowired
    private TestSetRepository testSetRepository;
    @Autowired
    private StuAndSetRepository stuAndSetRepository;

    @Override
    public Integer save(TestSetForm testSetForm) throws ParseException {
        TestSet testSet;
        if (testSetForm.getSetId() == null){
            testSet = new TestSet();
        }else {
            testSet = testSetRepository.findById(testSetForm.getSetId()).get();
        }
        BeanUtils.copyProperties(testSetForm,testSet);
        testSet.setStartTime(MyDateUtil.string2Date(testSetForm.getStartTime()));
        testSet.setOverTime(MyDateUtil.string2Date(testSetForm.getOverTime()));
        TestSet testSet1 = testSetRepository.save(testSet);
        StuAndSet stuAndSet = new StuAndSet();
        stuAndSet.setStuId(testSetForm.getTeachId());
        stuAndSet.setSetId(testSet1.getSetId());
        stuAndSet.setOpen("yes");
        stuAndSetRepository.save(stuAndSet);
        return testSet1.getSetId();
    }

    @Override
    public void delete(TestSetForm testSetForm){
        Integer setId = testSetForm.getSetId();
        boolean b = testSetRepository.findById(setId).isPresent();
        if (b){
            testSetRepository.deleteBySetId(setId);
        }throw new CommonException(ResultEnum.FAIL);

    }

    @Override
    public StuSetVO find(Integer setId){
        if (setId!=null){
            TestSet testSet;
            StuSetVO stuSetVO = new StuSetVO();
            boolean b = testSetRepository.findById(setId).isPresent();
            testSet = testSetRepository.findById(setId).get();
            if (b){
                String startTime = MyDateUtil.date2String(testSet.getStartTime());
                String overTime = MyDateUtil.date2String(testSet.getOverTime());
                stuSetVO.setStartTime(startTime);
                stuSetVO.setOverTime(overTime);
                BeanUtils.copyProperties(testSet,stuSetVO);
                return stuSetVO;
            }throw new CommonException(ResultEnum.FAIL);

        }throw new CommonException(ResultEnum.FAIL);
    }

    @Override
    public List<StuSetVO> findAll() {
        List<TestSet> testSetList = testSetRepository.findAll();
        List<StuSetVO> stuSetVOList = new ArrayList<>();
        for (TestSet testSet : testSetList) {
            StuSetVO stuSetVO = new StuSetVO();
            String startTime = MyDateUtil.date2String(testSet.getStartTime());
            String overTime = MyDateUtil.date2String(testSet.getOverTime());
            stuSetVO.setStartTime(startTime);
            stuSetVO.setOverTime(overTime);
            BeanUtils.copyProperties(testSet,stuSetVO);
            stuSetVOList.add(stuSetVO);
        }
        return stuSetVOList;
    }

    @Override
    public List<StuSetVO> findByTeachId(String teachId) {
        List<TestSet> testSetList = testSetRepository.findAllByTeachId(teachId);
        List<StuSetVO> stuSetVOList = new ArrayList<>();
        for (TestSet testSet : testSetList) {
            StuSetVO stuSetVO = new StuSetVO();
            String startTime = MyDateUtil.date2String(testSet.getStartTime());
            String overTime = MyDateUtil.date2String(testSet.getOverTime());
            stuSetVO.setStartTime(startTime);
            stuSetVO.setOverTime(overTime);
            BeanUtils.copyProperties(testSet,stuSetVO);
            stuSetVOList.add(stuSetVO);
        }
        return stuSetVOList;
    }
}
